<template>
  <div class="page-container">
    <div class="page-header">
      <div class="page-tile">{{ $t('workOrder.ReportedWorkorder') }}</div>
      <div class="left-wrapper"></div>
      <div class="right-wrapper"></div>
    </div>
    <el-scrollbar class="page-content">
      <div class="content-wrapper">
        <el-card>
          <basic-table :pageMethod="(pageQuery) => {
            pageQuery.search = searchItem;
            if (tableSearch) {
                pageQuery.page = 1;
                tableSearch = false;
              }
            pageQuery.declare = true;
            return $pandaApi.apiGetWorkorders(pageQuery);
          }">
            <template #table-options="tableSlotData">
              <div class="table-left-options">
                <el-button class="icon-button setting-button" style="width: auto;" :disabled="tableSlotData.table.selectRows.length <= 0" @click="batchCancel(tableSlotData.table.selectRows)">
                  <i class="el-icon-close" style="font-size: 12px; margin-right: 5px;"></i>
                  <span style="font-size: 12px;">{{ $t("workOrder.CancelReport") }}</span>
                </el-button>
              </div>
              <div class="table-right-options">
                <el-input :placeholder="$t('workOrder.searchReported')" v-model="searchItem" @keyup.enter.native="tableSearchChange(tableSlotData)" style="width:200px">
                </el-input>
                <el-button @click="tableSearchChange(tableSlotData)" type="primary" style="margin-left: 10px;" >{{ $t('common.search') }}</el-button>
              </div>
            </template>
            <template #table-column="tableSlotData">
              <el-table-column type="selection" width="40" align="center"></el-table-column>
              <el-table-column
                :label="$t('workOrder.postTime')"
              >
                <template #default="props">
                  <span>{{parseTime(new Date(props.row.created_at))}}</span>
                </template>
              </el-table-column>
              <el-table-column :label="$t('workOrder.postUser')" >
                <template #default="props">
                  <span>{{ props.row.user_name }}</span>
                </template>
              </el-table-column>
              <el-table-column
                :label="$t('workOrder.theme')"

              >
                <template #default="props">
                  <span>{{ $t("workOrder.function." + props.row.function) }}</span>
                </template>
              </el-table-column>
              <el-table-column :label="$t('workOrder.detail')" prop="arguments" width="80">
                <template #default="props">
                    <el-link slot="reference" style="font-size:12px" :underline="false" type="primary" @click="initData(props.row)">{{$t('common.details')}}</el-link>
                </template>
              </el-table-column>
              <el-table-column :label="$t('common.action')" width="180">
                <template #default="props">
                  <link-button :content="$t('workOrder.CancelReport')" :size="10" :font-size="'12px'" style="margin-right: 15px;" @click="batchCancel([props.row])"/>
                </template>
              </el-table-column>
            </template>
            <template #others-elements="tableSlotData">
              <approval-dialop ref="ApprovalDialop" :submit-method="(type, items, approval_comments) => {
                const ids = items.map((row) => row.id);
                return $pandaApi.apiBatchHandleWorkorders(ids, type, approval_comments).then(() => {
                  tableSlotData.table.refresh();
                });
              }" />
              <delete-dialog ref="DeleteDialog" :delete-method="(itemList) => $pandaApi.apiCancelReportWorkorder(itemList).then(() => {
                tableSlotData.table.deleteRefresh(itemList.length);
              })" />

              <order-details ref="OrderDetails"></order-details>
            </template>
          </basic-table>
        </el-card>
      </div>
    </el-scrollbar>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import BasicTable from '../../components/Basic/Table/BasicTable.vue';
import LinkButton from "../../components/Basic/Button/LinkButton.vue";
import { Language } from '../../utils/auth';
import { parseTime } from '../../utils';
import KeyValue from '../../components/Basic/KeyValue/KeyValue.vue';
import ApprovalDialop from './components/ApprovalDialop.vue';
import DeleteDialog from './components/DeleteDialog.vue';
import { AuthModule } from '../../store/modules/auth';
import OrderDetails from "./components/OrderDetails.vue";
export default Vue.extend({
  components: {
    BasicTable,
    LinkButton,
    KeyValue,
    ApprovalDialop,
    DeleteDialog,
    OrderDetails
  },
  computed: {
    Language: () => Language,
    parseTime: () => parseTime,
    AuthModule: () => AuthModule
  },
  data() {
    return {
      replyLoading: false, // 答复
      hangUpLoading: false, // 挂起
      reportLoading: false, // 上报
      searchItem: '',
      tableSearch: false
    }
  },
  methods: {
    initData(data: any) {
      let res = JSON.parse(JSON.stringify(data));
      (this as any).$refs.OrderDetails.init(res);
    },
    // 页面刷新重置page
    tableSearchChange(table: any) {
      (this as any).tableSearch = true;
      table.table.refresh(true);
    },
    batchCancel(selectRows: any) {
      (this as any).$refs.DeleteDialog.init({
        title: (this as any).$t('workOrder.cancelReportSelectedTips'),
        itemList: selectRows
      });
    },
    cancelRow(row: any) {
      (this as any).$refs.DeleteDialog.init({
        title: (this as any).$t("workOrder.cancelReportTips"),
        itemList: [row]
      });
    }
  }
})
</script>
<style lang="scss" scoped>
.page-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #EFF1F4;

  .page-header {
    height: 60px;
    background-color: #FFFFFF;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #D9D9D9;

    .page-tile {
      color: #031129;
      font-size: 16px;
      margin-left: 20px;

      .iconzhongyiicon_gaojing {
        margin-left: 5px;
      }
    }
  }

  .page-content {
    height: calc(100vh - 110px);
    overflow: hidden;
    flex-grow: 1;

    .content-wrapper {
      padding: 15px;
    }
  }
}

.table-left-options {
  display: flex;
}

.table-right-options {
  display: flex;

  .setting-button {
    margin-right: 10px;
  }
}
</style>
